package com.lwf.fatapi.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.lwf.fatapi.entities.SysRole;
import com.lwf.fatapi.service.SysRoleService;
import com.lwf.fatapi.utils.Result;
import com.mybatisflex.core.paginate.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 系统角色控制器
 *
 * @author lwf
 */
@Tag(name = "角色管理", description = "系统角色管理相关接口")
@RestController
@RequestMapping("/system/roles")
public class SysRoleController {

    @Autowired
    private SysRoleService roleService;

    @Operation(summary = "创建角色")
    @PostMapping
    @SaCheckPermission("system:role:add")
    public Result createRole(@RequestBody @Validated SysRole role) {
        boolean result = roleService.createRole(role);
        return result ? Result.success("角色创建成功") : Result.error("角色创建失败");
    }

    @Operation(summary = "更新角色")
    @PutMapping("/{id}")
    @SaCheckPermission("system:role:edit")
    public Result updateRole(
            @Parameter(description = "角色ID") @PathVariable Long id,
            @RequestBody @Validated SysRole role) {
        boolean result = roleService.updateRole(id, role);
        return result ? Result.success("角色更新成功") : Result.error("角色更新失败");
    }

    @Operation(summary = "删除角色")
    @DeleteMapping("/{id}")
    @SaCheckPermission("system:role:delete")
    public Result deleteRole(@Parameter(description = "角色ID") @PathVariable Long id) {
        boolean result = roleService.deleteRole(id);
        return result ? Result.success("角色删除成功") : Result.error("角色删除失败");
    }

    @Operation(summary = "根据ID查询角色")
    @GetMapping("/{id}")
    @SaCheckPermission("system:role")
    public Result<SysRole> getRoleById(@Parameter(description = "角色ID") @PathVariable Long id) {
        SysRole role = roleService.getRoleById(id);
        return Result.success(role);
    }

    @Operation(summary = "分页查询角色列表")
    @GetMapping("/page")
    @SaCheckPermission("system:role")
    public Result<Page<SysRole>> getRolePage(
            @Parameter(description = "页码") @RequestParam(defaultValue = "1") int pageNo,
            @Parameter(description = "每页大小") @RequestParam(defaultValue = "10") int pageSize,
            @Parameter(description = "关键词") @RequestParam(required = false) String keyword) {
        Page<SysRole> result = roleService.getRolePage(pageNo, pageSize, keyword);
        return Result.success(result);
    }

    @Operation(summary = "查询所有角色列表")
    @GetMapping("/list")
    @SaCheckPermission("system:role")
    public Result<List<SysRole>> getAllRoles() {
        List<SysRole> result = roleService.getAllRoles();
        return Result.success(result);
    }

    @Operation(summary = "根据用户ID查询角色列表")
    @GetMapping("/user/{userId}")
    @SaCheckPermission("system:role")
    public Result<List<SysRole>> getRolesByUserId(@Parameter(description = "用户ID") @PathVariable Long userId) {
        List<SysRole> result = roleService.getRolesByUserId(userId);
        return Result.success(result);
    }

    @Operation(summary = "启用/禁用角色")
    @PostMapping("/{id}/status")
    @SaCheckPermission("system:role:edit")
    public Result toggleRoleStatus(
            @Parameter(description = "角色ID") @PathVariable Long id,
            @Parameter(description = "状态") @RequestParam Integer status) {
        boolean result = roleService.toggleRoleStatus(id, status);
        return result ? Result.success("状态修改成功") : Result.error("状态修改失败");
    }
}
